<template>
  <el-dialog title="样品选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
  <div class="app-container">
    <!--搜索-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产物名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入产物名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
                        v-model="queryParams.startTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--列表-->
    <el-table
      ref="singleTable"
      :data="goodsList"
      highlight-current-row
      border
      style="width: 100%"
      @current-change="handleCurrentChange"
      @row-dblclick="handleRowDbClick"
    >
      <el-table-column label="产物编号" align="center" prop="goodsId" />
      <el-table-column label="产物名称" align="center" prop="goodsName" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>
    <div style="margin-top: 20px">
      <el-button @click="setCurrent(goodsList[1])">选中第二行</el-button>
      <el-button @click="setCurrent()">取消选择</el-button>
    </div>

    <!--分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div>
      <el-button type="primary" @click="confirmSelect">确定</el-button>
      <el-button @click="showFlag=false">取消</el-button>
    </div>

      </div>
  </el-dialog>
</template>

<script>
import { listGoods,
  getGoods,
  delGoods,
  addGoods,
  updateGoods
} from "@/api/product/goods";

export default {
  name: "index",
  data() {
    return {
      showFlag: false,
      //选中数据
      selectGoodsId: undefined,
      selectedRow: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产物表格数据
      goodsList: [],
      timeValue: [],
      pickerOptions:{
        disabledDate(time){
          return time.getTime() < Date.now() - 8.64e7;
        }
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产物列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 选择 */
    setCurrent(row) {
      console.log(row,222)
      this.$refs.singleTable.setCurrentRow(row);
    },
    /** 切换数据 */
    handleCurrentChange(val) {
      this.selectedRow = val;
    },

    /** 双击选择 */
    handleRowDbClick(row){
      if (row){
        this.selectedRow = row;
        console.log(this.selectedRow,222)
        this.showFlag = false;
        this.$emit('onSelected',this.selectedRow);
      }
    },

    /** 确定选中 */
    confirmSelect(){
      if (this.selectedRow === undefined){
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected',this.selectedRow);
      console.log(JSON.stringify(this.selectedRow));
      this.showFlag = false;
    }

  }
};
</script>
